% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Replication "Deconstructing the Yield Curve"
% Crump and Gospodinov (2024)
% Date: 26-JUL-2024
% Code to make csv files for all simulation tables
%   with external predictors
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
clc;
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SETTINGS
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alph = 0.10;
dispPref = '%3.3f';
%
fileTag1 = 'VAR1ExtPred';
fileTag2 = '3Fac_S5000_T600';
fileTag3 = 'BC';
%fileTag3 = 'NoBC';
%
data = load(['../output/sims', fileTag1, fileTag3, '_', fileTag2]);
%
mats = {'2y', '3y', '4y', '5y', '6y', '7y', '8y', '9y', '10y'};
%
size_CG = squeeze(mean(data.pval_Size_CG < alph));
size_BH = squeeze(mean(data.pval_Size_BH < alph));
size_FullOracle = squeeze(mean(data.pval_Size_FullOracle < alph));
%
power_CG = squeeze(mean(data.pval_Power_CG < alph));
power_BH = squeeze(mean(data.pval_Power_BH < alph));
power_FullOracle = squeeze(mean(data.pval_Power_FullOracle < alph));
%
[m,k] = size(size_CG(2:end,2:end));
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BOOTSTRAP METHODS
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
if strcmp(fileTag3,'BC')
    tab_size = strings(m,3*k+1);
    % Two external predictors
    tab_power = strings(m,3*(k-2)+1);
    %
    tmp_size = [size_CG(2:end,2:end) size_BH(2:end,2:end) size_FullOracle(2:end,2:end)];
    tmp_power = [power_CG(2:end,2:end) power_BH(2:end,2:end) power_FullOracle(2:end,2:end)];
elseif strcmp(fileTag3,'NoBC')
    tab_size = strings(m,2*k+1);
    % Two external predictors
    tab_power = strings(m,2*(k-2)+1);    
    tmp_size = [size_CG(2:end,2:end) size_BH(2:end,2:end) size_FullOracle(2:end,2:end)];
    tmp_power = [power_CG(2:end,2:end) power_BH(2:end,2:end) power_FullOracle(2:end,2:end)];
end    
%
tab_size(:,1) = mats;
tab_power(:,1) = mats;
%
for i = 1:width(tmp_size), tab_size(:,i+1) = num2str(round(tmp_size(:,i),k),dispPref); end
for i = 1:width(tmp_power), tab_power(:,i+1) = num2str(round(tmp_power(:,i),k-2),dispPref); end    
    
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ASYMPTOTIC STANDARD ERRORS
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
size_NW = squeeze(mean(abs(data.tstat_Size_NW) > norminv(1-alph/2),1));
power_NW = squeeze(mean(abs(data.tstat_Size_NW) > norminv(1-alph/2),1));

size_EWC = squeeze(mean(abs(data.tstat_Size_LLSW_EWC) > tinv(1-alph/2,data.sp.nu_LLSW_EWC),1));
power_EWC = squeeze(mean(abs(data.tstat_Power_LLSW_EWC) > tinv(1-alph/2,data.sp.nu_LLSW_EWC),1));

if alph == 0.1
    cv_LLSW_NW_10 = load('../input/cv_nw_10.mat');
    [~, idx] = min(abs(data.sp.S_LLSW_NW/data.sp.T-cv_LLSW_NW_10.cv_nw_10(:,1,1)));
    size_NW_LLSW = squeeze(mean(abs(data.tstat_Size_LLSW_NW) > sqrt(cv_LLSW_NW_10.cv_nw_10(idx,2,1)),1));
    power_NW_LLSW = squeeze(mean(abs(data.tstat_Power_LLSW_NW) > sqrt(cv_LLSW_NW_10.cv_nw_10(idx,2,1)),1));
elseif alph == 0.05
    cv_LLSW_NW_05 = load('../input/cv_nw_05.mat');
    [~, idx] = min(abs(data.sp.S_LLSW_NW/data.sp.T-cv_LLSW_NW_05.cv_nw_05(:,1,1)));
    size_NW_LLSW = squeeze(mean(abs(data.tstat_Size_LLSW_NW) > sqrt(mean(cv_LLSW_NW_05.cv_nw_05(idx,2,1))),1));
    power_NW_LLSW = squeeze(mean(abs(data.tstat_Power_LLSW_NW) > sqrt(mean(cv_LLSW_NW_05.cv_nw_05(idx,2,1))),1));
end

tab_size_LLSW = strings(m,3*k+1);
tab_power_LLSW = strings(m,3*(k-2)+1);

tmp_size_LLSW = [size_NW(2:end,2:end) size_EWC(2:end,2:end) size_NW_LLSW(2:end,2:end)];
tmp_power_LLSW = [power_NW(2:end,2:end) power_EWC(2:end,2:end) power_NW_LLSW(2:end,2:end)];

tab_size_LLSW(:,1) = mats;
for i = 1:width(tmp_size_LLSW), tab_size_LLSW(:,i+1) = num2str(round(tmp_size_LLSW(:,i),3),dispPref); end
tab_power_LLSW(:,1) = mats;
for i = 1:width(tmp_power_LLSW), tab_power_LLSW(:,i+1) = num2str(round(tmp_power_LLSW(:,i),3),dispPref); end

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SAVE CSV FILES
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alphTag = num2str(100*alph);
%
if strcmp(fileTag3,'BC')
    writematrix(tab_size, ['../output/TableSimsSize_', fileTag1, '_BC_', fileTag2, '_alpha', alphTag, '.csv']);
    writematrix(tab_power, ['../output/TableSimsPower_', fileTag1, '_BC_', fileTag2, '_alpha', alphTag, '.csv']);
    writematrix(tab_size_LLSW, ['../output/TableSimsSize_', fileTag1, '_LLSW_', fileTag2, '_alpha', alphTag, '.csv']);
    writematrix(tab_power_LLSW, ['../output/TableSimsPower_', fileTag1, '_LLSW_', fileTag2, '_alpha', alphTag, '.csv']);
elseif strcmp(fileTag3,'NoBC')
    writematrix(tab_size, ['../output/TableSimsSize_', fileTag1, '_noBC_', fileTag2, '_alpha', alphTag, '.csv']);
    writematrix(tab_power, ['../output/TableSimsPower_', fileTag1, '_noBC_', fileTag2, '_alpha', alphTag, '.csv']);
end

